package com.wyd.wenyongdaAdmin.xo.admin.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.wenyongdaAdmin.commons.model.admin.user.dto.UserInfoDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.user.entity.UserInfoEntity;
import com.wyd.wenyongdaAdmin.commons.model.admin.user.vo.UserInfoVO;
import com.wyd.wenyongdaAdmin.xo.admin.user.mapper.UserInfoMapper;
import com.wyd.wenyongdaAdmin.xo.admin.user.service.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements IUserInfoService {

    private static final Logger log = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Override
    public Integer addUserInfo(UserInfoDTO userInfoDTO) {
        if (ObjectUtil.isNotNull(userInfoDTO)) {
            UserInfoEntity userInfo = new UserInfoEntity();
            BeanUtil.copyProperties(userInfoDTO, userInfo);
            return this.baseMapper.insert(userInfo);
        } else {
            return 0;
        }
    }

    @Override
    public PageInfo<UserInfoVO> selectUserInfoVOPageWithCount(Integer pageNumber, Integer pageSize, UserInfoVO userInfoVO) {
        if (pageNumber != 0 && pageSize != 0) {
            PageHelper.startPage(pageNumber, pageSize);
            List<UserInfoVO> userInfoVOList = this.baseMapper.selectUserInfoVO(userInfoVO);
            PageInfo<UserInfoVO> userInfoVOPageInfo = new PageInfo<>(userInfoVOList);
            PageHelper.clearPage();
            return userInfoVOPageInfo;
        } else {
            return null;
        }
    }

    @Override
    public UserInfoVO selectUserInfoVOById(Long id) {
        if (id > 0) {
            return this.baseMapper.selectUserInfoVO(new UserInfoVO().setId(id)).get(0);
        } else {
            return null;
        }
    }

    @Override
    public Integer updateUserInfo(UserInfoDTO userInfoDTO) {
        if (ObjectUtil.isNotNull(userInfoDTO)) {
            UserInfoEntity userInfo = new UserInfoEntity();
            BeanUtil.copyProperties(userInfoDTO, userInfo);
            return this.baseMapper.updateById(userInfo);
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteUserInfo(List<Long> ids) {
        if (ObjectUtil.isNotNull(ids) && ids.size() > 0) {
            return this.baseMapper.deleteBatchIds(ids);
        } else {
            return 0;
        }
    }
}
